package cn.tedu.oa_system.service.impl;

import cn.tedu.oa_system.base.json_result.ResultDataDto;
import cn.tedu.oa_system.base.json_result.ResultDto;
import cn.tedu.oa_system.base.util.SessionUtil;
import cn.tedu.oa_system.mapper.NoteListMapper;
import cn.tedu.oa_system.mapper.TypeListMapper;
import cn.tedu.oa_system.pojo.NoteList;
import cn.tedu.oa_system.pojo.TypeList;
import cn.tedu.oa_system.pojo.in.AddNewNoteIn;
import cn.tedu.oa_system.pojo.in.NoteListIn;
import cn.tedu.oa_system.pojo.in.TypeListIn;
import cn.tedu.oa_system.pojo.model.UserInfo;
import cn.tedu.oa_system.service.NoteService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

import static cn.tedu.oa_system.base.constant.DataBaseConstants.DATABASE_AFFECTED_ROW;
import static cn.tedu.oa_system.base.constant.TypeConstant.TYPE_MODEL_NOTE;

@Slf4j
@Service
public class NoteServiceImpl implements NoteService {


    @Autowired
    private NoteListMapper noteListMapper;

    @Autowired
    private TypeListMapper typeListMapper;

    @Autowired
    private SessionUtil sessionUtil;

    @Override
    public ResultDataDto<NoteList> list(NoteListIn noteListIn) {
        String userId = noteListIn.getUserId();
        LambdaQueryWrapper<NoteList> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NoteList::getCreatemanId,userId);
        List<NoteList> noteLists = noteListMapper.selectList(queryWrapper);
        ResultDataDto<NoteList> listResultDataDto = new ResultDataDto<>();
        listResultDataDto.setIsSuccess(true);
        listResultDataDto.setDataList(noteLists);
        return listResultDataDto;
    }

    @Override
    public ResultDataDto<TypeList> typeList() {
        LambdaQueryWrapper<TypeList> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TypeList::getTypeModel,TYPE_MODEL_NOTE);
        List<TypeList> typeLists = typeListMapper.selectList(queryWrapper);
        ResultDataDto<TypeList> resultDataDto = new ResultDataDto<>();
        resultDataDto.setDataList(typeLists);
        resultDataDto.setIsSuccess(true);
        return resultDataDto;
    }

    @Override
    public ResultDto<Void> addNewNoteType(TypeListIn typeListIn) {
        String typeModel = typeListIn.getTypeModel();
        Assert.isTrue(StringUtils.isAllBlank(typeModel),"不能指定新增笔记类型的typeModel!");
        typeListIn.setTypeName(TYPE_MODEL_NOTE);
        typeListIn.setTypeName(typeListIn.getTypeName());
        TypeList typeList = new TypeList();
        BeanUtils.copyProperties(typeListIn,typeList);
        int row = typeListMapper.insertType(typeList);
        Assert.isTrue(row==DATABASE_AFFECTED_ROW,"新增笔记类型失败请稍后充实");
        ResultDto<Void> resultDto = new ResultDto<>();
        resultDto.setIsSuccess(true);
        resultDto.setMes("新增成功");
        return resultDto;
    }

    @Override
    public ResultDto<Void> addNewNote(AddNewNoteIn addNewNoteIn) {
        log.info("调用保存笔记接口,当前入参为:[{}]",addNewNoteIn);
        NoteList noteList = new NoteList();
        BeanUtils.copyProperties(addNewNoteIn,noteList);
        noteList.setNoteId(null);
        noteList.setCreateTime(new Date());
        UserInfo userInfo = sessionUtil.getUserInfo();
        log.info("从缓存中获取到当前用户信息为[{}]",userInfo);
        noteList.setCreatemanId(userInfo.getId());
        ResultDto<Void> resultDto = new ResultDto<>();
        resultDto.setIsSuccess(true);
        resultDto.setMes("保存成功");
        return resultDto;
    }

}
